﻿using Clock.Models;
using Dotnet.Code;
using Dotnet.Code.Extend;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using My.Dotnet.Net;
using Wutos.App;
using Wutos.Domain;

namespace Clock.Controllers.Api
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public partial class StockController : ControllerBase
    {
        private readonly ILogger<StockController> _logger;

        public StockController(ILogger<StockController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public JsonResult StockInfo(string code,string? name) {
            try
            {
                Models.StockRoot stockRoot = GetNetStockInfo(code, name);
                return new JsonResult(new { Cur = stockRoot.Result.cur, PanKouInfo = stockRoot.Result.pankouinfos })
                {
                    ContentType = "application/json; charset=utf-8"
                };
            }
            catch (Exception ex)
            {
                return new JsonResult(new { Code = 500, Message = ex.Message})
                {
                    ContentType = "application/json; charset=utf-8"
                };
            }

        }
        [HttpGet]
        public JsonResult Sum(string? code,string? price,string? increase)
        {
            StockApp stock=new StockApp();
            List<StockEntity> list = new List<StockEntity>();
            StockSum stockSum=new StockSum();   
            if (string.IsNullOrEmpty(code)) {
                list = stock.GetList();
            }
            else
            {
                list = stock.GetList(code);
                stockSum=CaclSingleStockSum(list);

            }
            return new JsonResult(stockSum) { 
                ContentType= "application/json; charset=utf-8"
            };
        }

        [HttpPost]
        public JsonResult Add([FromBody]StockEntity entity)
        {

            return null;
        }
        [HttpGet]
        public JsonResult List()
        {
            StockApp stock = new StockApp();
            List<StockEntity> list = stock.GetList();
            var dis=(from item in list group item by new { item.证券名称,item.证券代码}  into grp select new
            {
                grp.Key.证券名称,
                grp.Key.证券代码,
                数量=grp.Sum(item=>item.成交数量)
            }).ToList();
            var dis1 = (from item in dis orderby item.数量 descending,item.证券名称 ascending select item).ToList();
            return new JsonResult(dis1)
            {
                ContentType = "application/json;charset=utf-8"
            };
        }
    }
}
